package com.hibase.core.page.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.page.entity.advance.TabsLayout;
import com.hibase.core.page.entity.editxml.CardLayout;
import com.hibase.core.page.entity.listxml.ListCustomSearchXmlVO;
import com.hibase.core.page.entity.listxml.ListCustomSearchsXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleCallSelectXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleDateRangeXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleDateXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleEnumXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleTextXmlVO;
import com.hibase.core.page.entity.modulexml.ModuleVO;
import com.hibase.core.page.entity.modulexml.vo.ModuleShowColumnVO;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 描述: PageConfig 数据处理工具类
 *
 * @author li-wei-hua
 * @date 2021-05-08 9:55
 */
@Slf4j
public class PageConfigUtils {

	/**
	 * 处理查询控件
	 * 
	 * @param customSearchs
	 * @return
	 */
	public static List<ListCustomSearchXmlVO> disposeListCustomSearch(ListCustomSearchsXmlVO customSearchs) {

		List<ListCustomSearchXmlVO> customSearch = Lists.newArrayList();

		// 获取控件
		List<ModuleTextXmlVO> textList = customSearchs.getTextList(); // 文本
		List<ModuleDateXmlVO> dateList = customSearchs.getDateList(); // 时间
//		List<ModuleEnumXmlVO> selectList = customSearchs.getEnumList(); // 字典下拉
		List<ModuleDateRangeXmlVO> dateRangeList = customSearchs.getDateRangeList(); // 时间范围
		List<ModuleCallSelectXmlVO> callSelectList = customSearchs.getCallSelectList(); // 远程下拉
		customSearchs.setCallSelectList(callSelectList);
//		List<ModuleTextAreaXmlVO> textAreaList = customSearchs.getTextAreaList(); // 文本域
		List<ModuleEnumXmlVO> enumList = customSearchs.getEnumList(); // 枚举
//		List<ModuleNumberXmlVO> numberList = customSearchs.getNumberList(); // 数字

		// 处理控件
		if (CollUtil.isNotEmpty(textList)) {
			List<ListCustomSearchXmlVO> text = textList.stream().map(p -> {
				ListCustomSearchXmlVO vo = EntityUtil.transformByJson(p, ListCustomSearchXmlVO.class);
				vo.setType("text");
				vo.setName(p.getTitle());
				vo.setCode(p.getDataIndex());
				return vo;
			}).collect(Collectors.toList());
			customSearch.addAll(text);
		}
		if (CollUtil.isNotEmpty(dateList)) {
			List<ListCustomSearchXmlVO> date = dateList.stream().map(p -> {
				ListCustomSearchXmlVO vo = EntityUtil.transformByJson(p, ListCustomSearchXmlVO.class);
				vo.setType("date");
				vo.setName(p.getTitle());
				vo.setCode(p.getDataIndex());
				return vo;
			}).collect(Collectors.toList());
			customSearch.addAll(date);
		}
		if (CollUtil.isNotEmpty(enumList)) {
			List<ListCustomSearchXmlVO> select = enumList.stream().map(p -> {
				ListCustomSearchXmlVO vo = EntityUtil.transformByJson(p, ListCustomSearchXmlVO.class);
				vo.setType("enum");
				vo.setName(p.getTitle());
				vo.setCode(p.getDataIndex());
				vo.setValue(p.getEnumType());
				return vo;
			}).collect(Collectors.toList());
			customSearch.addAll(select);
		}
		if (CollUtil.isNotEmpty(dateRangeList)) {
			List<ListCustomSearchXmlVO> select = dateRangeList.stream().map(p -> {
				ListCustomSearchXmlVO vo = EntityUtil.transformByJson(p, ListCustomSearchXmlVO.class);
				List<ModuleShowColumnVO> columns = Lists.newArrayList();
				columns.add(new ModuleShowColumnVO(p.getStartTime(), p.getStartTime()));
				columns.add(new ModuleShowColumnVO(p.getEndTime(), p.getEndTime()));
				vo.setType("dateRange");
				vo.setName(p.getTitle());
				vo.setCode(p.getDataIndex());
				vo.setColumns(columns);
				return vo;
			}).collect(Collectors.toList());
			customSearch.addAll(select);
		}
		// 远程下拉框
		if (CollUtil.isNotEmpty(callSelectList)) {
			List<ListCustomSearchXmlVO> select = callSelectList.stream().map(p -> {
				ListCustomSearchXmlVO vo = EntityUtil.transformByJson(p, ListCustomSearchXmlVO.class);
				String[] titleArr = p.getShowTitles().split(",");
				String[] columnArr = p.getShowColumns().split(",");
				List<ModuleShowColumnVO> columns = Lists.newArrayList();
				for (int i = 0; i < titleArr.length; i++) {
					columns.add(new ModuleShowColumnVO(columnArr[i], titleArr[i]));
				}
				if (null != p.getChilds()) {
					vo.setChildrens(p.getChilds().getChildren());
				}
				if (null != p.getParams()) {
					vo.setParams(p.getParams().getParam());
				}
				vo.setColumns(columns);
				vo.setType("callSelect");
				vo.setName(p.getTitle());
				vo.setCode(p.getDataIndex());
				vo.setShowName(p.getShowName());
				return vo;
			}).collect(Collectors.toList());
			customSearch.addAll(select);
		}

		// TODO 后续控件待补充处理
		log.info("success");
		return customSearch;
	}

	/**
	 * 处理编辑控件
	 * 
	 * @param cardLayoutList
	 * @return
	 */
	public static List<CardLayout> disposeEditCardLayout(List<CardLayout> cardLayoutList) {

		cardLayoutList.forEach(card -> {
		
			List<ModuleVO> allModuleList = Lists.newArrayList();

			if (CollUtil.isNotEmpty(card.getTextList())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getTextList(), ModuleVO.class);
				modules.forEach(t -> t.setType("text"));
				allModuleList.addAll(modules);
				card.setTextList(null);
			}
			if (CollUtil.isNotEmpty(card.getDateList())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getDateList(), ModuleVO.class);
				modules.forEach(t -> t.setType("date"));
				allModuleList.addAll(modules);
				card.setDateList(null);
			}
			if (CollUtil.isNotEmpty(card.getDateRangeList())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getDateRangeList(), ModuleVO.class);
				modules.forEach(t -> t.setType("dateRange"));
				allModuleList.addAll(modules);
				card.setDateRangeList(null);
			}
			if (CollUtil.isNotEmpty(card.getEnumList())) {
				List<ModuleVO> modules = card.getEnumList().stream().map(t -> {
					ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
					moduleVO.setType("enum");
					if (null != t.getChilds()) {
						moduleVO.setChildrens(t.getChilds().getChildren());
					}
					return moduleVO;
				}).collect(Collectors.toList());
			
				allModuleList.addAll(modules);
				card.setEnumList(null);
			}

			if (CollUtil.isNotEmpty(card.getCallSelectList())) {
				List<ModuleVO> modules = card.getCallSelectList().stream().map(t -> {
					ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
					String[] titleArr = t.getShowTitles().split(",");
					String[] columnArr = t.getShowColumns().split(",");
					List<ModuleShowColumnVO> columns = Lists.newArrayList();
					for (int i = 0; i < titleArr.length; i++) {
						columns.add(new ModuleShowColumnVO(columnArr[i], titleArr[i]));
					}
					moduleVO.setColumns(columns);
					moduleVO.setType("callSelect");
					moduleVO.setParam(t.getParam());
					
					if (null != t.getChilds()) {
						moduleVO.setChildrens(t.getChilds().getChildren());
					}
					if (null != t.getParams()) {
						moduleVO.setParams(t.getParams().getParam());
					}
					if (null != t.getMulti()) {
						moduleVO.setMulti(t.getMulti());
					}
					return moduleVO;
				}).collect(Collectors.toList());

				allModuleList.addAll(modules);
				card.setCallSelectList(null);
			}
			if (CollUtil.isNotEmpty(card.getNumberList())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getNumberList(), ModuleVO.class);
				modules.forEach(t -> t.setType("number"));
				allModuleList.addAll(modules);
				card.setNumberList(null);
			}
			if (CollUtil.isNotEmpty(card.getTextAreaList())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getTextAreaList(), ModuleVO.class);
				modules.forEach(t -> t.setType("textArea"));
				allModuleList.addAll(modules);
				card.setTextAreaList(null);
			}
			if (CollUtil.isNotEmpty(card.getAttachmentList())) {
				List<ModuleVO> modules = card.getAttachmentList().stream().map(t -> {
					ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
                    moduleVO.setType(StrUtil.isBlank(t.getType()) ? "attachment" : t.getType());
					moduleVO.setParam(t.getParam());
					
					if (null != t.getParams()) {
						moduleVO.setParams(t.getParams().getParam());
					}
					return moduleVO;
				}).collect(Collectors.toList());
				
				allModuleList.addAll(modules);
				card.setAttachmentList(null);
			}
			if (CollUtil.isNotEmpty(card.getDivider())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getDivider(), ModuleVO.class);
				allModuleList.addAll(modules);
				modules.forEach(t -> t.setType("divider"));
				card.setDivider(null);
			}
			//多选
			if (CollUtil.isNotEmpty(card.getCheckboxList())) {
				List<ModuleVO> modules = card.getCheckboxList().stream().map(t -> {
					ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
					moduleVO.setType("checkbox");
					moduleVO.setOptions(t.getOptions().getOption());
					return moduleVO;
				}).collect(Collectors.toList());
				
				allModuleList.addAll(modules);
				modules.forEach(t -> t.setType("checkbox"));
				card.setCheckboxList(null);
			}
			//init事件
			// add by liuyulei  2022-01-22 事件应该与 moduleList 为同一级别
			if (ObjectUtil.isNotEmpty(card.getPageEvent())) {
				card.getPageEvent().getSetDataUI().forEach(e -> {
					if(e.getParam() != null && e.getParam().getParam() != null && e.getParam().getParam().size() > 0) {
						e.setParams(e.getParam().getParam());
					}
					e.setParam(null);
				});
			}
			//图片组件
			if (CollUtil.isNotEmpty(card.getImgXmlVO())) {
				List<ModuleVO> modules = EntityUtil.transformByJson(card.getImgXmlVO(), ModuleVO.class);
				allModuleList.addAll(modules);
				modules.forEach(t -> t.setType("img"));
				card.setImgXmlVO(null);
			}
			//单输入框 级联
			if (CollUtil.isNotEmpty(card.getCascader())) {
				List<ModuleVO> modules = card.getCascader().stream().map(t -> {
					ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
					if (null != t.getParams()) {
						moduleVO.setParams(t.getParams().getParam());
					}
					moduleVO.setType("cascader");
					return moduleVO;
				}).collect(Collectors.toList());
				allModuleList.addAll(modules);
				card.setCascader(null);
			}
			if (CollUtil.isNotEmpty(allModuleList)) {
				card.setModuleList(allModuleList);
				//降序排序
				Collections.sort(allModuleList, Comparator.comparing(ModuleVO::getRow, Comparator.nullsFirst(Comparator.naturalOrder())));
				;
			}
		});

		return cardLayoutList;
	}
	
	/**
	 * 
	 * @Title: PageConfigUtils
	 * @Description: 处理编辑控件tabsLayout
	 * @author TimBrian
	 * @since May 27, 2021 10:06:24 AM
	 * @param tabsLayoutList
	 * @return
	 */
	public static List<TabsLayout> disposeTabsCardLayout(List<TabsLayout> tabsLayoutList) {
		
		tabsLayoutList.forEach(card -> {

			card.getTabPane().forEach(tabPane -> {

				List<ModuleVO> allModuleList = Lists.newArrayList();

				if (CollUtil.isNotEmpty(tabPane.getTextList())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getTextList(), ModuleVO.class);
					modules.forEach(t -> t.setType("text"));
					allModuleList.addAll(modules);
					tabPane.setTextList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getDateList())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getDateList(), ModuleVO.class);
					modules.forEach(t -> t.setType("date"));
					allModuleList.addAll(modules);
					tabPane.setDateList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getDateRangeList())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getDateRangeList(), ModuleVO.class);
					modules.forEach(t -> t.setType("dateRange"));
					allModuleList.addAll(modules);
					tabPane.setDateRangeList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getEnumList())) {
					List<ModuleVO> modules = tabPane.getEnumList().stream().map(t -> {
						ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
						moduleVO.setType("enum");
						if (null != t.getChilds()) {
							moduleVO.setChildrens(t.getChilds().getChildren());
						}
						return moduleVO;
					}).collect(Collectors.toList());
				
					allModuleList.addAll(modules);
					tabPane.setEnumList(null);
				}

				if (CollUtil.isNotEmpty(tabPane.getCallSelectList())) {
					List<ModuleVO> modules = tabPane.getCallSelectList().stream().map(t -> {
						ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
						String[] titleArr = t.getShowTitles().split(",");
						String[] columnArr = t.getShowColumns().split(",");
						List<ModuleShowColumnVO> columns = Lists.newArrayList();
						for (int i = 0; i < titleArr.length; i++) {
							columns.add(new ModuleShowColumnVO(columnArr[i], titleArr[i]));
						}
						moduleVO.setColumns(columns);
						moduleVO.setType("callSelect");
						moduleVO.setShowName(t.getShowName());
						if (null != t.getChilds()) {
							moduleVO.setChildrens(t.getChilds().getChildren());
						}
						if (null != t.getParams()) {
							moduleVO.setParams(t.getParams().getParam());
						}
						if (null != t.getMulti()) {
							moduleVO.setMulti(t.getMulti());
						}
						moduleVO.setParam(t.getParam());
						return moduleVO;
					}).collect(Collectors.toList());

					allModuleList.addAll(modules);
					tabPane.setCallSelectList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getNumberList())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getNumberList(), ModuleVO.class);
					modules.forEach(t -> t.setType("number"));
					allModuleList.addAll(modules);
					tabPane.setNumberList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getTextAreaList())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getTextAreaList(), ModuleVO.class);
					modules.forEach(t -> t.setType("textArea"));
					allModuleList.addAll(modules);
					tabPane.setTextAreaList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getAttachmentList())) {
					
					List<ModuleVO> modules = tabPane.getAttachmentList().stream().map(t -> {
						ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
						moduleVO.setType(StrUtil.isBlank(t.getType()) ? "attachment" : t.getType());
						moduleVO.setParam(t.getParam());
						
						if (null != t.getParams()) {
							moduleVO.setParams(t.getParams().getParam());
						}
						return moduleVO;
					}).collect(Collectors.toList());
					
					allModuleList.addAll(modules);
					tabPane.setAttachmentList(null);
				}
				if (CollUtil.isNotEmpty(tabPane.getDivider())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getDivider(), ModuleVO.class);
					allModuleList.addAll(modules);
					modules.forEach(t -> t.setType("divider"));
					tabPane.setDivider(null);
				}
				//多选
				if (CollUtil.isNotEmpty(tabPane.getCheckboxList())) {
					List<ModuleVO> modules = tabPane.getCheckboxList().stream().map(t -> {
						ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
						moduleVO.setType("checkbox");
						moduleVO.setOptions(t.getOptions().getOption());
						return moduleVO;
					}).collect(Collectors.toList());
					
					allModuleList.addAll(modules);
					modules.forEach(t -> t.setType("checkbox"));
					tabPane.setCheckboxList(null);
				}
				//init事件
				if (ObjectUtil.isNotEmpty(tabPane.getPageEvent())) {
					tabPane.getPageEvent().getSetDataUI().forEach(e -> {
						if(e.getParam() != null && e.getParam().getParam() != null && e.getParam().getParam().size() > 0) {
							e.setParams(e.getParam().getParam());
						}
						e.setParam(null);
					});
				}
				//图片组件
				if (CollUtil.isNotEmpty(tabPane.getImgXmlVO())) {
					List<ModuleVO> modules = EntityUtil.transformByJson(tabPane.getImgXmlVO(), ModuleVO.class);
					allModuleList.addAll(modules);
					modules.forEach(t -> t.setType("img"));
					tabPane.setImgXmlVO(null);
				}
				//单输入框 级联
				if (CollUtil.isNotEmpty(tabPane.getCascader())) {
					List<ModuleVO> modules = tabPane.getCascader().stream().map(t -> {
						ModuleVO moduleVO = EntityUtil.transformByJson(t, ModuleVO.class);
						if (null != t.getParams()) {
							moduleVO.setParams(t.getParams().getParam());
						}
						moduleVO.setType("cascader");
						return moduleVO;
					}).collect(Collectors.toList());
					allModuleList.addAll(modules);
					tabPane.setCascader(null);
				}
				if (CollUtil.isNotEmpty(allModuleList)) {
					tabPane.setModuleList(allModuleList);
					//降序排序
//					Collections.sort(allModuleList, Comparator.comparing(ModuleVO :: getRow));
					Collections.sort(allModuleList, Comparator.comparing(ModuleVO::getRow, Comparator.nullsFirst(Comparator.naturalOrder())));
				}
			});
		});
		
		return tabsLayoutList;
	}

}
